package com.gurun.camera.device;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.danale.player.SPlayer;
import com.danale.player.window.ScreenType;
import com.danale.sdk.Danale;
import com.danale.sdk.device.constant.FlipType;
import com.danale.sdk.device.service.BaseCmdResponse;
import com.danale.sdk.device.service.request.SetColorRequest;
import com.danale.sdk.device.service.request.SetFlipRequest;
import com.danale.sdk.device.service.request.SetVideoRequest;
import com.danale.sdk.device.service.response.SetVideoResponse;
import com.danale.sdk.platform.entity.device.Device;
import com.danale.video.sdk.device.result.SetColorInfoResult;
import com.gurun.camera.R;
import com.gurun.camera.R2;
import com.gurun.camera.device.constant.VideoDataType;
import com.gurun.camera.device.presenter.impl.VideoPresenter;
import com.gurun.camera.main.cache.DeviceCache;
import com.gurun.camera.main.model.MainModelImpl;
import com.gurun.camera.main.presenter.MainPresenter;
import com.gurun.camera.main.presenter.MainPresenterImpl;
import com.gurun.camera.main.view.MainView;
import com.gurun.camera.utils.ToastUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class VideoActivity extends BaseVideoActivity implements MainView {

    private String mDeviceId = "";
    private String mRoomName = "";
    private Device mDevice;
    FlipType flipType = FlipType.HORIZONTAL;

    @BindView(R2.id.splayer)
    SPlayer splayer;

    public static void startActivity(Context context, String id, String roomName) {
        Intent intent = new Intent();
        intent.setClass(context, VideoActivity.class);
        intent.putExtra("device_id", id);
        intent.putExtra("room_name", roomName);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        unbinder = ButterKnife.bind(this);
        mDeviceId = getIntent().getStringExtra("device_id");
        mRoomName = getIntent().getStringExtra("room_name");
        initMainPresenter();
    }

    private void initData() {
        mDevice = DeviceCache.getInstance().getDevice(mDeviceId);
        if (mDevice == null) {
            ToastUtil.showShortToast(mContext,"设备不存在");
            finish();
            return;
        }
        videoPresenter = new VideoPresenter(this, VideoDataType.ONLINE_IPC, splayer);
        initView();
        initPlayer();
    }

    private void initView() {

    }

    private void initPlayer() {

        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);

        videoPresenter.initPlay(displayMetrics.widthPixels, (float) 16 / (float) 9, ScreenType.One);    //Set the aspect ratio
        videoPresenter.setData(mDeviceId);
        videoPresenter.prepare();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                videoPresenter.startVideo();
            }
        }, 200);
        setFlip(FlipType.TURN180);
        setVideoQuality(100);
        setVideoColor();
    }

    /**
     * 设置翻转
     *
     * @param flipType
     */
    private void setFlip(FlipType flipType) {
        SetFlipRequest setFlipRequest = new SetFlipRequest();
        setFlipRequest.setCh_no(1);
        setFlipRequest.setFlip_type(flipType);
        Danale.get().getDeviceSdk().command().setFlip(mDevice.getCmdDeviceInfo(), setFlipRequest)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseCmdResponse>() {
                    @Override
                    public void call(BaseCmdResponse baseCmdResponse) {

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    /**
     * 设置视频质量
     */
    private void setVideoQuality(int quality) {
        SetVideoRequest setVideoRequest = new SetVideoRequest();
        setVideoRequest.setCh_no(1);
        setVideoRequest.setVideo_quality(quality);
        Danale.get().getDeviceSdk().command().setVideo(mDevice.getCmdDeviceInfo(), setVideoRequest)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<SetVideoResponse>() {
                    @Override
                    public void call(SetVideoResponse setVideoResponse) {

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    /**
     * 设置视频亮度
     */
    private void setVideoColor() {
        SetColorRequest setColorRequest = new SetColorRequest();
        setColorRequest.setCh_no(1);
        setColorRequest.setBrightness(0);
        Danale.get().getDeviceSdk().command().setColor(mDevice.getCmdDeviceInfo(), setColorRequest)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseCmdResponse>() {
                    @Override
                    public void call(BaseCmdResponse setVideoResponse) {

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    //==================获取列表=====================
    private MainPresenter mainPresenter;

    private void initMainPresenter() {
        mainPresenter = new MainPresenterImpl(this, new MainModelImpl());
        mainPresenter.loadDevicesRemote();
    }

    @Override
    public void showEmptyPage() {

    }

    @Override
    public void showDeviceListPage(List<Device> list) {
        initData();
    }

    @Override
    public void showLoading() {
//        showProgressDialog();
    }

    @Override
    public void hideLoading() {
//        hideProgressDialog();
    }

    @Override
    public void showError(String err) {
//        hideProgressDialog();
    }

    @Override
    public void showErrorPage() {
//        hideProgressDialog();
    }

    @Override
    public void notifyLogoutState(String state) {

    }
}